<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: transcripts.php 131 2011-06-12 20:24:56Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
$full_header    = 1;
$body['class']  = 'row2';
$header         = '';
require($libdir . "init." . $phpEx);
require($libdir . "rooms." . $phpEx);
require($libdir . "chat_functs." . $phpEx);
require($libdir . "login_code." . $phpEx);
require($libdir . "transcripts." . $phpEx);

//print("<!-- op == {$_REQUEST['op']}, roomlist = {$_REQUEST['roomlist']} -->\n");

if (!isset($_REQUEST['op']) || $_REQUEST['op'] == "") {
    //print("<!-- showing login, empty op -->\n");
    show_login("");
    exit;
}

if ($_REQUEST['roomlist'] == '' && $_REQUEST['room_name'] == '') {
    //print("<!-- showing login, empty roomlist -->\n");
    show_login("Please select a valid room.");
    exit;
}

if ($_REQUEST['room_name'] != '') { 
    //print("<!-- getting room info for {$_REQUEST['room_name']}... -->\n");
    $room   = getRoomInfoByName($_REQUEST['room_name']);
    $rid    = $room['room_id'];
} else {
    //print("<!-- getting room info for {$_REQUEST['roomlist']}... -->\n");
    $room   = getRoomInfo($_REQUEST['roomlist']);
    $rid    = $_REQUEST['roomlist'];
}

if ($rid == '') {
    show_login("Please select a valid room.");
    exit;
}

//print("<!-- checking authorization... rid = $rid-->\n");
auth_login($room);
//print("<!-- after authorization... -->\n");

$start  = "'{$_REQUEST['year1']}-{$_REQUEST['month1']}-{$_REQUEST['day1']} {$_REQUEST['hour1']}:{$_REQUEST['minute1']}:00'";
$end    = "'{$_REQUEST['year2']}-{$_REQUEST['month2']}-{$_REQUEST['day2']} {$_REQUEST['hour2']}:{$_REQUEST['minute2']}:59'";
$events = pull_transcript_events($start, $end, $rid, 'base');

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'transcript_body.tpl'));

$vars   = array();
while (list($k, $v) = each($room)) {
    $vars[strtoupper($k)] = $v;
}
$vars['DATE1']  = stripslashes($start);
$vars['DATE2']  = stripslashes($end);
$template->assign_vars($vars);

foreach ($events as $row) {
    print("<!-- id = {$row['event_id']} -->");
    $date_fmt   = '';
    if (isset($_REQUEST['show_timestamps']) && $_REQUEST['show_timestamps'] == 'on') {
        //print("[" . $row['event_date_fmt'] . "] ");
        $date_fmt   = "[" . $row['event_date_fmt'] . "] ";
    }
    //print(stripslashes($row['event_text']) . "</i></b></font><br />\n");

    $template->assign_block_vars('events', array(
        'EVENT_ID'      => $row['event_id'],
        'EVENT_DATE'    => $date_fmt,
        'EVENT_TEXT'    => stripslashes($row['event_text'])
    ));
}

$template->pparse('body');
